package com.sunshine.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Optional;
import java.util.Properties;

/**
 * Created by along on 4/17/18.
 */
public class AppProps {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppProps.class);
    private static Properties properties;

    static {
        try {
            Resource resource = new ClassPathResource("/application.properties");
            properties = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException e) {
            LOGGER.error("AppProps init error: {}", e.getMessage());
        }
    }

    public static Optional<String> get(String key) {
        //NOTICE
        // properties maybe null
        return Optional.ofNullable(properties.getProperty(key));
    }
}
